package com.quanxi.qxaimain.service.indicatorsService;

import com.alibaba.fastjson.JSONObject;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.quanxi.qxaimain.common.globalException.GlobalException;
import com.quanxi.qxaimain.common.globalException.ResultCode;
import com.quanxi.qxaimain.common.util.sqlFormat.sqlFormat;
import com.quanxi.qxaimain.dao.indicators.indicatorsDataMapper;
import com.quanxi.qxaimain.dao.produce.produceMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Author lyx
 * @create 2022/9/2 16:59
 */
@Service("indicatorsService")
public class indicatorsServiceImpl implements indicatorsService {
    @Resource
    private indicatorsDataMapper indicatorsDataMapper;
    @Resource
    private produceMapper produceMapper;

    @Override

    public  List<Map<String, Object>>  indicatorsLists(String table, List<String> indIdList) throws GlobalException  {

        List<Map<String, Object>> indicatorsLists = indicatorsDataMapper.find_indicators_sql_lists_ok(table, indIdList);//去掉失败与未调试sql
        if (indicatorsLists.size()==0){
            throw new GlobalException("请检查该编号是否满足条件： select  * from qxzsk_t_indicator_sql where  IFNULL(sql_detail_value,'') not REGEXP '失败' and IFNULL(sql_detail_value,'') not REGEXP '未调试' and LENGTH(sql_value) >0 " +
                    "and IFNULL(sql_value,'') not REGEXP '失败' and IFNULL(sql_value,'') not REGEXP '未调试' ", ResultCode.INDICATOR_NUM_NULL.getCode(),ResultCode.INDICATOR_NUM_NULL.getMsg());
        }

        return indicatorsLists;
    }

    @Override
    public JSONObject indicatorsDetailSql(String sql,String D1,String D2,String dept_code ,int pageNum, int pageSize) {
        JSONObject jsonObject=new JSONObject();
        Object sql_result_all= new Object();
        String sql_result_detail="";
        long starTime = System.currentTimeMillis();
        if (sql.length()>0){
            String  sqlformat= sqlFormat.sqlFormat(sql,D1,D2,dept_code);
            try{
                   PageHelper.startPage(pageNum, pageSize);//分页
                   List<LinkedHashMap<String, Object>> linkedHashMaps = indicatorsDataMapper.exec_sql(sqlformat);
                   PageInfo<LinkedHashMap<String, Object>> pageinfo = new PageInfo(linkedHashMaps);

                if (linkedHashMaps.size()==0){
                    sql_result_all=null;
                }
                else {
                 //   sql_result_all= JSON.toJSONString(pageinfo.getList(), SerializerFeature.WriteMapNullValue);
                    sql_result_all=pageinfo;
                }
                sql_result_detail = "sql成功";

            }catch (Exception e){
                String error= e.getMessage();
                sql_result_detail="sql失败:"+error;
                //  sql="sql不正确";
            }
        }
        long  endTime=System.currentTimeMillis();
        jsonObject.put("sql",sql);
        jsonObject.put("sql_result",sql_result_detail);//
        jsonObject.put("sql_result_all",sql_result_all);//分页数据
        jsonObject.put("run_time", (endTime-starTime)+"ms");//执行时间);
        return jsonObject;
    }


    @Override
    public JSONObject indicatorsDetailSqlTest(String sql,String D1,String D2,String dept_code ,int pageNum, int pageSize) {
        JSONObject jsonObject=new JSONObject();
        //     String sql_result_all="";
        String  sqlformat="";
        String sql_result_detail="";
        long starTime = System.currentTimeMillis();
        if (sql.length()>0){
              sqlformat= sqlFormat.sqlFormat(sql,D1,D2,dept_code);
            try{
                PageHelper.startPage(1, 1);
                indicatorsDataMapper.exec_sql(sqlformat);
                sql_result_detail = "sql成功";
            }catch (Exception e){
                String error= e.getMessage();
                sql_result_detail="sql失败:"+error;
            }
        }
        long  endTime=System.currentTimeMillis();
        jsonObject.put("sql",sql);
        jsonObject.put("sqlformat",sqlformat);
        jsonObject.put("sql_result",sql_result_detail);//
        //   jsonObject.put("sql_result_all",sql_result_all);//全部数据
        jsonObject.put("run_time", (endTime-starTime)+"ms");//执行时间);
        return jsonObject;
    }

    @Override
    public JSONObject indicatorsDetailPorduce(String porduceName, String D1, String D2, String dept_code,int pageNum, int pageSize) {
        JSONObject jsonObject=new JSONObject();
        Object sql_result_all= new Object();
        String sql_result_detail="";
        long starTime = System.currentTimeMillis();
        if (porduceName.length()>0){
            try{
                Map<String, Object> map = new HashMap<>();
                map.put("D1", D1);
                map.put("D2", D2);
//                map.put("PAGESIZE", pageSize);
//                map.put("PAGENUM", pageNum);
                map.put("produceName", porduceName);
                produceMapper.produce(map);
                if ((Integer) map.get("ErrCode")==0){
                    List<LinkedHashMap<String, Object>> list=  (List<LinkedHashMap<String, Object>>)map.get("p_cur");
                    if (list.size()==0){
                        sql_result_all=null;
                    }
                    else {

                        Page page = new Page(pageNum, pageSize);//分页
                        int total =list.size();
                        page.setTotal(total);
                        int startIndex = (pageNum - 1) * pageSize;
                        int endIndex = Math.min(startIndex + pageSize, total);
                        page.addAll(list.subList(startIndex, endIndex));
                        PageInfo pageInfo = new PageInfo<>(page);
                    //    sql_result_all= JSON.toJSONString(o, SerializerFeature.WriteMapNullValue);
                        sql_result_all=pageInfo;
                    }
                    sql_result_detail = "sql成功";
                }

                if ((Integer) map.get("ErrCode")==1){
                    sql_result_detail="sql失败:"+ map.get("ErrStr").toString();
                }

            }catch (Exception e){
                String error= e.getMessage();
                sql_result_detail="sql失败:"+error;

            }
        }
        long  endTime=System.currentTimeMillis();
        jsonObject.put("sql",porduceName);
        jsonObject.put("sql_result",sql_result_detail);//
        jsonObject.put("sql_result_all",sql_result_all);//分页数据
        jsonObject.put("run_time", (endTime-starTime)+"ms");//执行时间);
        return jsonObject;

    }

    @Override
    public JSONObject indicatorsDetailPorduceTest(String porduceName, String D1, String D2, String dept_code,int pageNum, int pageSize) {
        JSONObject jsonObject=new JSONObject();
        //    String sql_result_all="";
        String sql_result_detail="";
        long starTime = System.currentTimeMillis();
        if (porduceName.length()>0){
            try{
                Map<String, Object> map = new HashMap<>();
                map.put("D1", D1);
                map.put("D2", D2);
                map.put("PAGESIZE", pageSize);
                map.put("PAGENUM", pageNum);
                map.put("produceName", porduceName);
                produceMapper.produce(map);
                if ((Integer) map.get("ErrCode")==0){
                    sql_result_detail = "sql成功";
                }
                if ((Integer) map.get("ErrCode")==1){
                    sql_result_detail="sql失败:"+ map.get("ErrStr").toString();
                }

            }catch (Exception e){
                String error= e.getMessage();
                sql_result_detail="sql失败:"+error;

            }
        }
        long  endTime=System.currentTimeMillis();
        jsonObject.put("sqlformat",porduceName);
        jsonObject.put("sql",porduceName);
        jsonObject.put("sql_result",sql_result_detail);//
        //    jsonObject.put("sql_result_all",sql_result_all);//全部数据
        jsonObject.put("run_time", (endTime-starTime)+"ms");//执行时间);
        return jsonObject;

    }

    public JSONObject indicatorsDetailSqlNoPage(String sql,String D1,String D2,String dept_code ) {
        JSONObject jsonObject=new JSONObject();
        Object sql_result_all= new Object();
        String sql_result_detail="";
        long starTime = System.currentTimeMillis();
        if (sql.length()>0){
            String  sqlformat= sqlFormat.sqlFormat(sql,D1,D2,dept_code);
            try{
              //  PageHelper.startPage(pageNum, pageSize);//分页
                List<LinkedHashMap<String, Object>> linkedHashMaps = indicatorsDataMapper.exec_sql(sqlformat);
             //   PageInfo<LinkedHashMap<String, Object>> pageinfo = new PageInfo(linkedHashMaps);

                if (linkedHashMaps.size()==0){
                    sql_result_all=null;
                }
                else {
                    //   sql_result_all= JSON.toJSONString(pageinfo.getList(), SerializerFeature.WriteMapNullValue);
                    sql_result_all=linkedHashMaps;
                }
                sql_result_detail = "sql成功";

            }catch (Exception e){
                String error= e.getMessage();
                sql_result_detail="sql失败:"+error;
                //  sql="sql不正确";
            }
        }
        long  endTime=System.currentTimeMillis();
        jsonObject.put("sql",sql);
        jsonObject.put("sql_result",sql_result_detail);//
        jsonObject.put("sql_result_all",sql_result_all);//
        jsonObject.put("run_time", (endTime-starTime)+"ms");//执行时间);
        return jsonObject;
    }

    public JSONObject indicatorsDetailPorduceNoPage(String porduceName, String D1, String D2, String dept_code) {
        JSONObject jsonObject=new JSONObject();
        Object sql_result_all= new Object();
        String sql_result_detail="";
        long starTime = System.currentTimeMillis();
        if (porduceName.length()>0){
            try{
                Map<String, Object> map = new HashMap<>();
                map.put("D1", D1);
                map.put("D2", D2);
//                map.put("PAGESIZE", pageSize);
//                map.put("PAGENUM", pageNum);
                map.put("produceName", porduceName);
                produceMapper.produce(map);
                if ((Integer) map.get("ErrCode")==0){
                    List<LinkedHashMap<String, Object>> list=  (List<LinkedHashMap<String, Object>>)map.get("p_cur");
                    if (list.size()==0){
                        sql_result_all=null;
                    }
                    else {

                     //   Page page = new Page(pageNum, pageSize);//分页
                     //   int total =list.size();
                      //  page.setTotal(total);
                      //  int startIndex = (pageNum - 1) * pageSize;
                     //   int endIndex = Math.min(startIndex + pageSize, total);
                     //   page.addAll(list.subList(startIndex, endIndex));
                    //    PageInfo pageInfo = new PageInfo<>(page);
                        //    sql_result_all= JSON.toJSONString(o, SerializerFeature.WriteMapNullValue);
                        sql_result_all=list;
                    }
                    sql_result_detail = "sql成功";
                }

                if ((Integer) map.get("ErrCode")==1){
                    sql_result_detail="sql失败:"+ map.get("ErrStr").toString();
                }

            }catch (Exception e){
                String error= e.getMessage();
                sql_result_detail="sql失败:"+error;

            }
        }
        long  endTime=System.currentTimeMillis();
        jsonObject.put("sql",porduceName);
        jsonObject.put("sql_result",sql_result_detail);//
        jsonObject.put("sql_result_all",sql_result_all);//分页数据
        jsonObject.put("run_time", (endTime-starTime)+"ms");//执行时间);
        return jsonObject;

    }
}

